// pages/my/my.js
const app = getApp()
var url = require('../../utils/url.js');
var base64 = require('../../utils/base64.js');
var token;
// console.log(url)
// console.log(base64.Base64)
var userinfo;
var formid;
var shareuSer=0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
     order:[
       { icon: "../../images/my/order_01.png", name: "待付款" },
       { icon: "../../images/my/order_02.png", name: "待发货" },
       { icon: "../../images/my/order_03.png", name: "待收货" },
       { icon: "../../images/my/order_04.png", name: "待评价" },
       { icon: "../../images/my/order_05.png", name: "售后" },
     ],
    listItem:[
      { icon: "../../images/my/list_01.png", name: "积分幸运星活动记录" },
      { icon: "../../images/my/list_08.png", name: "积分商城订单" },
      { icon: "../../images/my/list_07.png", name: "中奖记录" },
      { icon: "../../images/my/list_02.png", name: "我的积分" },
      { icon: "../../images/my/list_03.png", name: "我的收藏" },
      { icon: "../../images/my/list_04.png", name: "我的优惠券" },
      // { icon: "../../images/my/list_05.png", name: "我的分享" },
      { icon: "../../images/my/list_06.png", name: "收货地址" },
      { icon: "../../images/my/list_09.png", name: "我的粉丝" },
    ],
    orderNum:""
  },
  submit: function (e) {
    console.log(e.detail.formId);
    formid = e.detail.formId
    this.setData({
      formid: formid
    })
  },
  GotUserInfo: function (e) {
    var that = this;
    // console.log(e)
    wx.getStorage({
      key: 'rec_id',
      success: function (res) {
        shareuSer = res.data
        app.onGotUserInfo(e, that, formid, shareuSer)
      },
      fail: function () {
        shareuSer = 0
        app.onGotUserInfo(e, that, formid, shareuSer)
      }
    })
  },
  toPersonage:function(e){
    var that=this;
    formid = e.detail.formId
    token = wx.getStorageSync("token");
    this.setData({
      formid: formid
    })
    var idx = e.currentTarget.dataset.index;

    console.log(e)
    if (token){
      if (idx == 0) {
        wx.navigateTo({
          url: '/pages/myActive/myActive',
        })
      } else if (idx == 2) {
        wx.navigateTo({
          url: '/pages/myLucky/myLucky',
        })
      } else if (idx == 3) {
        wx.navigateTo({
          url: '/pages/myJifen/myJifen',
        })
      } else if (idx == 4) {
        wx.navigateTo({
          url: '/pages/myCollect/myCollect',
        })
      } else if (idx == 5) {
        wx.navigateTo({
          url: '/pages/myCoupon/myCoupon',
        })
      } else if (idx == 6) {
        wx.navigateTo({
          url: '/pages/myadr/myadr',
        })
      } else if (idx == 1) {
        wx.navigateTo({
          url: '/pages/myexchage/myexchage',
        })
      } else if (idx == 7) {
        wx.navigateTo({
          url: '/pages/fensi/fensi',
        })
      } 
    }else{
      wx.getStorage({
        key: 'rec_id',
        success: function (res) {
          shareuSer = res.data
          app.onGotUserInfo(e, that, formid, shareuSer)
        },
        fail: function () {
          shareuSer = 0
          app.onGotUserInfo(e, that, formid, shareuSer)
        }
      })
      
    }
    
  },
  toOrder:function(e){
    var idx = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '/pages/myOrder/myOrder?type='+idx,
    })
  },
  getDate(){
    var that=this;
    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/count',
      data: {},
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res.data)
        var msg=res.data.msg;
        wx.hideLoading();
        if(res.data.code==200){
          that.setData({
            orderNum:res.data.data
          })
        }else if(res.data.code==401){
           wx.showToast({
             title: '请先登录',
             icon: 'none',
           })
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
          })
        }
      },fail(){
        wx.hideLoading();
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  toSetting:function(){
    wx.navigateTo({
      url: '/pages/mysetting/mysetting'
    })
    // if(token){

    // }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        that.setData({
          userinfo: res.data
        })
      },
    })
    wx.getStorage({
      key: 'token',
      success: function (res) {
        that.setData({
          token: res.data
        })
        token=res.data;
        that.getDate()
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})